<?php
class Comment extends AppModel {

    var $name = 'Comment';
    var $validate = array(
        'id' => VALID_NUMBER,
        'created' => VALID_NOT_EMPTY,
        'text' => VALID_NOT_EMPTY,
        'article_id' => VALID_NUMBER,
        'user_name' => VALID_NOT_EMPTY,
        'title' => VALID_NOT_EMPTY,
        'captcha' => array(
            'rule' => array('captcha','captcha'),
            'message' => array('Captcha answer is not correct')
        ),
        'badinput' => array(
            'rule' => array('blank'),
            'on' => 'create'
        ),
        'user_ip' => array(
            'rule' => array('blacklist','user_ip')
        )
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
            'Article' =>
        array('className' => 'Article',
                        'foreignKey' => 'article_id',
                        'conditions' => '',
                        'fields' => '',
                        'order' => '',
                        'counterCache' => ''
        ),

            'User' =>
        array('className' => 'User',
                        'foreignKey' => 'user_id',
                        'conditions' => '',
                        'fields' => '',
                        'order' => '',
                        'counterCache' => ''
        ),

    );

    function captcha($data, $fieldName) {
        $valid = false;

        if(isset($fieldName))
        {
            if (strtolower($data['captcha']) == Configure::read('Captcha.answer')) {
                $valid = true;
            }
        }
        return $valid;
    }

    function blacklist($data, $fieldname) {
        App::Import('Model', 'Blacklist');
        $blacklist = new Blacklist();
        return !$blacklist->check($data['user_ip']);
    }
}
?>